//
//  LHCMainViewController.m
//  MyAddressBook
//
//  Created by lihongcheng on 16/4/22.
//  Copyright © 2016年 lihongcheng. All rights reserved.
//

#import "LHCMainViewController.h"
#import "LHCAddPeopleViewController.h"
#import "LHCDetailsViewController.h"

@interface LHCMainViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,strong) UITableView *tableView;
@property (nonnull,strong) NSMutableArray *dataArray;

@end

@implementation LHCMainViewController

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.dataArray = [[NSMutableArray alloc] init];
    self.dataArray = [LHCSqlOperate readDataFromSql];
    [self.tableView reloadData];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self createMainView];
    
}

-(void)createMainView
{
    UIBarButtonItem *rightBtn = [[UIBarButtonItem alloc] initWithTitle:@"添加" style:
                                                                UIBarButtonItemStylePlain target:self action:@selector(rightBtnClick:)];
    self.navigationItem.rightBarButtonItem = rightBtn;
    
    self.navigationItem.title = @"个人信息";
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self.view addSubview:self.tableView];
    
    
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    static NSString *strID  = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:strID];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:strID];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    LHCDataModel *model = self.dataArray[indexPath.row];
    cell.textLabel.text = [NSString stringWithFormat:@"%@",model.name];
    cell.detailTextLabel.text = [NSString stringWithFormat:@"%@",model.phone];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    LHCDetailsViewController *dvc = [[LHCDetailsViewController alloc] init];
    dvc.model = self.dataArray[indexPath.row];
    [self.navigationController pushViewController:dvc animated:YES];
}

-(void)rightBtnClick:(UIBarButtonItem *)btn
{
    LHCAddPeopleViewController *apvc = [[LHCAddPeopleViewController alloc] init];
    [self.navigationController pushViewController:apvc animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
